<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Cate as CateModel;
use app\admin\model\Article as ArticleModel;
class Cate extends Common
{
	//前置操作
	protected $beforeActionList = [
        
        'delsancate'  =>  ['only'=>'delete'],
    ];

    public function lst()
    {
        $cate=new CateModel();

        if(request()->isPost()){
            $data=input('post.');
            foreach ($data as $k => $v) {
                $cate->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新成功！',url('cate/lst'));
            return;
        }
    	
    	$catelist=$cate->cateTree();
    	//dump($catelist);die;
    	$this->assign('cateres',$catelist);
        return view('list');
    }

    public function add()
    {
    	$cate=new CateModel();
    	if(request()->isPost()){
            $data=input('post.');
            dump( $data);die;
            $validate = \think\Loader::validate('Cate');//实例化link验证器
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
    		$res=$cate->save($data);
    		if($res){
    			$this->success('添加栏目成功!',url('cate/lst'));
    		}else{
    			$this->error('添加栏目失败！');
    		}
    	}
    	$catelist=$cate->cateTree();
    	$this->assign('catelist',$catelist);
        return view();
    }

    public function edit()
    {
    	$cate=new CateModel();
    	if(request()->isPost()){
            // echo '你想修改',input('id'),'栏目<br/>';
            // echo '想修改他的新爹为',input('pid'),'<br />';
            // echo input('pid'),'的家谱树是：';
            // print_r($cate->getTree(input('pid')));
            // exit;
    		$trees=$cate->getTree(input('pid'));
    		$flag=true;
                foreach ($trees as $v) {
                    if($v['id']==input('id')){
                        $flag=false;
                        break;
                    }
                }
                if(!$flag){
                    // echo $cat_id,'是',$data['parent_id'],'的祖先';
                    $this->error('上级栏目选取失败！');
                    exit;

                }

            $data=input('post.');
            $validate = \think\Loader::validate('Cate');//实例化link验证器
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
             $save=$cate->save($data,['id'=>$data['id']]);
             if($save !==false){
                $this->success('栏目修改成功！',url('cate/lst'));
             }else{
                $this->error('栏目修改失败！');
             } 

    		return;
    	}

    	//获取cate子孙树显示
    	$cates=$cate->find(input('id'));
    	$catelist=$cate->cateTree();
    	//dump($catelist);die;
    	$this->assign(array(
    		'catelist'=>$catelist,
    		'cates'=>$cates,
    		));
        return view();
    }

    //获取当前栏目的子栏目
    public function delete(){
    	$del=db('cate')->delete(input('id'));

    	if($del){
    			$this->success('删除栏目成功!',url('cate/lst'));
    		}else{
    			$this->error('删除栏目失败！');
    		}
    }

    public function  delsancate(){
    	$cateid=input('id');//要删除的当前栏目ID
    	$cate=new CateModel();
    	$sonid=$cate->getchildren($cateid);
        $allcateid=$sonid;
        $allcateid[]=$cateid;
        //循环删除栏目下面的所属文章，目前图片不能批量删除
        foreach ($allcateid as $k => $v) {
            $article=new ArticleModel();
            $article->where(array('cateid'=>$v))->delete();
        }
    	//dump($sonid);die;
    	if($sonid){
    		db('cate')->delete($sonid);
    	}
    }
}
